
window.exports = {
    // action = { code, type, payload }
    "fanyi": {
        mode: "none",
        args: {
            enter: (action) => {
                OnStart(BaiduFanyi, action)
            },
        }
    },
    "fanyiapi": {
        mode: "none",
        args: {
            enter: (action) => {
                OnStart(BaiduFanyiAPI, action)
            },
        }
    },
    "search": {
        mode: "none",
        args: {
            enter: (action) => {
                OnStart(BaiduSearch, action)
            }
        }
    },
    "yuque": {
        mode: "none",
        args: {
            enter: (action) => {
                OnStart(YuQue, action)
            }
        }
    }
}

/** 开始
 * 
 * @param {*} fun 接口
 * @param {*} action 参数
 */
function OnStart(fun, action) {
    window.utools.hideMainWindow()
    fun(action)
    window.utools.outPlugin()
}

//#region 百度接口

/** 打开百度翻译
 * 
 * @param {*} action 
 */
function BaiduFanyi(action) {
    let url = 'https://fanyi.baidu.com/mtpe-individual/multimodal'
    if (CheckAction(action))
        url += '?query=' + action.payload + '#/'

    window.utools.ubrowser.goto(url).run({ width: 1150, height: 600 })
}

/** 调用百度翻译API
 * 
 * @param {*} action 
 * @returns 
 */
function BaiduFanyiAPI(action) {
    if (!CheckAction(action)) return
    const axios = require('axios')

    //百度翻译token 每日 5W 字
    var appid = '20240914002150120'
    var key = 'qUKAEDrybL1ex81kzrOd'

    // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
    var url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
    var query = action.payload
    var salt = (new Date).getTime()

    var requestUrl = GetUrl(url, {
        q: query,
        appid: appid,
        salt: salt,
        from: 'auto',
        to: 'auto',
        sign: GetBaiduApiToken(appid, query, salt, key)
    })

    axios
        .get(requestUrl)
        .then(response => {
            //结果：response.data.trans_result[0].dst
            //异常：response.data.error_code
            var res = response.data.trans_result[0].dst
            window.utools.copyText(res)
            window.utools.showNotification(response.data.trans_result[0].dst)
        })
}

/** 百度一下
 * 
 * @param {*} action 
 */
function BaiduSearch(action) {
    let url = 'https://www.baidu.com'
    if (CheckAction(action))
        url += '/s?ie=utf-8&wd=' + action.payload

    window.utools.ubrowser.goto(url).run({ width: 1280, height: 720 })
}

//#endregion


/** Url
 * 
 * @param {*} url 地址
 * @param {*} data 参数
 * @returns 
 */
function GetUrl(url, data) {
    url += '?'
    Object.keys(data).forEach(key => {

        url += key + '=' + data[key] + '&'
    })

    return url.substring(0, url.length - 1)
}

/** 校验action
 * 
 * @param {*} action 
 * @returns 
 */
function CheckAction(action) {
    let value = action.payload
    if (value && value !== action.code)
        return true;
    else return false;
}

/** 计算百度API的token
 * 
 * @param {*} appid 
 * @param {*} query 
 * @param {*} salt 
 * @param {*} key 
 * @returns 
 */
function GetBaiduApiToken(appid, query, salt, key) {
    const md5 = require('./md5')
    var str1 = appid + query + salt + key
    return md5(str1)
}


//#region 语雀

function YuQue(action) {
    let url = 'https://www.yuque.com/'
    window.utools.ubrowser.goto(url).run({ width: 1280, height: 720 })
}

//#endregion